/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kn.cstf2.global.db.Operations;

import java.sql.*;
import kn.cstf2.global.Utils.ConectaProperties;
import kn.cstf2.global.Utils.CstF2Exception;
import java.util.ResourceBundle;
import kn.cstf2.global.db.props.Propiedades;
import java.util.Properties;

/**
 *
 * @author user1
 */
public class OperacionesDB {



    public Connection obtenerConexion(String dbSTK) {
        String nombrePro = "";
        Connection conn = null;
        if (dbSTK.equals("db")) {
            nombrePro = "/kn/cstf2/global/db/props/db.properties";
        } else {
            nombrePro = "/kn/cstf2/global/db/props/db2.properties";
        }
        
        Properties proper = Propiedades.obtenerPropiedades(nombrePro);
        String driver = proper.getProperty("jdbc.driver");
        String url = proper.getProperty("jdbc.url");
        String username = proper.getProperty("jdbc.username");
        String password = proper.getProperty("jdbc.password");
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException ex) {
            throw new CstF2Exception("Error al obtener la conexion", ex);
        } catch (ClassNotFoundException ex) {
            throw new CstF2Exception("No se encontro el Driver de la conexion", ex);
        }
    
        return conn;
    }
    
     public void cerrarConnection(Connection conn) {
        
         try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException ex) {
            throw new CstF2Exception("Error al cerrar la conexion", ex);
        }
    }

    public void iniciarTransaccion(Connection conn) {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.setAutoCommit(false);
            }
        } catch(SQLException ex) {
            throw new CstF2Exception("Error al iniciar la transaccion", ex);
        }
    }

    public void finalizarTransaccion(Connection conn) {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.setAutoCommit(true);
            }
        } catch(SQLException ex) {
            throw new CstF2Exception("Error al terminar la transaccion", ex);
        }
    }

    public void commit(Connection conn) {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.commit();
            }
        } catch(SQLException ex) {
            throw new CstF2Exception("Error al realizar el commit de la transaccion", ex);
        }
    }

    public void rollback(Connection conn) {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.rollback();
            }
        } catch(SQLException ex) {
            throw new CstF2Exception("Error al realizar el rollback de la transaccion", ex);
        }
    }
    
    public void cerrarResultSet(ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException ex) {

            throw new CstF2Exception("Error al intentar cerrar el ResultSet");
        }
    }

    public void cerrarStatement(Statement statement) {
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException ex) {

            throw new CstF2Exception("Error al intentar cerrar la Statement");
        }
    }
    
}
